package com.ruoyi.app.service.impl;

import cn.dev33.satoken.secure.BCrypt;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.NumberUtil;
import com.ruoyi.app.api.domain.AppUser;
import com.ruoyi.app.domain.bo.AppMerchantBo;
import com.ruoyi.app.domain.bo.AppUserBo;
import com.ruoyi.app.domain.bo.AppUserCardBo;
import com.ruoyi.app.domain.vo.AppUserCardVo;
import com.ruoyi.app.domain.vo.AppUserVo;
import com.ruoyi.app.service.IAppMerchantService;
import com.ruoyi.app.service.IAppNewUserService;
import com.ruoyi.app.service.IAppUserCardService;
import com.ruoyi.app.service.IAppUserService;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.enums.UserType;
import com.ruoyi.common.core.exception.user.UserException;
import com.ruoyi.common.core.utils.MessageUtils;
import com.ruoyi.system.api.RemoteDictService;
import com.ruoyi.system.api.domain.SysDictData;
import com.ruoyi.system.api.domain.SysUser;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

@RequiredArgsConstructor
@Service
public class AppNewUserServiceImpl implements IAppNewUserService {

    private final IAppUserService iAppUserService;

    private final IAppUserCardService iAppUserCardService;

    private final IAppMerchantService iAppMerchantService;

    @DubboReference
    private RemoteDictService remoteDictService;

    @Override
    public R<Object> registerAppNewUser(String phone, String password1, String password2) {
        if(!password1.equals(password2)){
            return R.fail("两次输入的密码不一致");
        }
        AppUser appUser = iAppUserService.queryByPhone(phone);
        if (appUser == null){
            AppUser appUserBo = new AppUser();
            appUserBo.setPhoneNumber(phone);
            appUserBo.setUserName(phone);
            appUserBo.setUserPassword(BCrypt.hashpw(password1));
            appUserBo.setUserLevel(0L);
            appUserBo.setUserType(0L);
            appUserBo.setUserRealStatus(0L);
            appUserBo.setCertificationStatus(0L);
            appUserBo.setStatus(1L);
            iAppUserService.saveUser(appUserBo);
            return R.ok();
        }else{
            return R.fail("该手机号已注册账户");
        }

    }

    @Override
    public List<SysDictData> selectDictDataByType(String dictType) {
        return remoteDictService.selectDictDataByType(dictType);
    }

    @Override
    public List<AppUserCardVo> selectUserCard(String id, String type) {
        AppUserCardBo bo = new AppUserCardBo();
        bo.setUserId(Convert.toLong(id));
        bo.setCardType(Convert.toLong(type));
        return iAppUserCardService.queryList(bo);
    }

    @Override
    public R<Object> appNewUserAuth(String id, String userIdName, String userIdCard, String cardNo, String cardBank,
                                    String bankBranch, String reservePhone, String idCardFront, String idCardBack) {
        AppUserVo appUser = iAppUserService.queryById(Convert.toLong(id));
        if (appUser != null){
            // 实名信息保存
            appUser.setUserIdName(userIdName);
            appUser.setUserIdCard(userIdCard);
            appUser.setUserIdCardPath(idCardFront);
            appUser.setUserIdCardPath2(idCardBack);
            appUser.setUserRealStatus(1L);
            AppUserBo update = BeanUtil.toBean(appUser, AppUserBo.class);
            iAppUserService.updateByBo(update);

            // 保存储蓄卡信息
            AppUserCardBo appUserCard = new AppUserCardBo();
            appUserCard.setCardNo(cardNo);
            appUserCard.setCardBank(cardBank);
            appUserCard.setOpeningBranch(bankBranch);
            appUserCard.setPhone(reservePhone);
            appUserCard.setCardType(0L); // 0 储蓄卡  1 信用卡
            appUserCard.setUserId(Convert.toLong(id));
            iAppUserCardService.insertByBo(appUserCard);
            return R.ok();
        }else{
            return R.fail("该手机号不存在");
        }
    }

    @Override
    public R<Object> appMerchantAuth(String id, String name, String priCategory, String contactName,
                                     String contactPhone, String businessLicense, String code) {
        AppUserVo appUser = iAppUserService.queryById(Convert.toLong(id));
        if (appUser != null){
            // 实名信息保存
            appUser.setCertificationStatus(1L);
            AppUserBo update = BeanUtil.toBean(appUser, AppUserBo.class);
            iAppUserService.updateByBo(update);

            // 保存商户信息
            AppMerchantBo appMerchant = new AppMerchantBo();
            appMerchant.setName(name);
            appMerchant.setPriCategory(priCategory);
            appMerchant.setContactName(contactName);
            appMerchant.setContactPhone(contactPhone);
            appMerchant.setBusinessLicense(businessLicense);
            appMerchant.setUserId(Convert.toLong(id));
            iAppMerchantService.insertByBo(appMerchant);
            return R.ok();
        }else{
            return R.fail("该手机号不存在");
        }
    }

    @Override
    public R<Object> addCreditCard(String id, String userIdName, String userIdCard, String cardNo, String cardBank,
                                   String phone, String cardDate, String cardEndorse) {
        AppUserCardVo appUserCardVo = iAppUserCardService.queryByCardNo(cardNo);
        if(appUserCardVo != null){
            return R.fail("当前卡号已存在");
        }
        if(cardDate.length() != 4 || !NumberUtil.isNumber(cardDate)){
            return R.fail("请填写正确的有效期");
        }
        // 保存信用卡信息
        AppUserCardBo appUserCard = new AppUserCardBo();
        appUserCard.setCardNo(cardNo);
        appUserCard.setCardBank(cardBank);
        appUserCard.setPhone(phone);
        appUserCard.setCardDate(cardDate);
        appUserCard.setCardEndorse(cardEndorse);
        appUserCard.setCardType(1L); // 0 储蓄卡  1 信用卡
        appUserCard.setUserId(Convert.toLong(id));
        iAppUserCardService.insertByBo(appUserCard);
        return R.ok();
    }

    @Override
    public R<Object> addBankCard(String id, String userIdName, String userIdCard, String cardNo, String cardBank, String phone) {
        AppUserCardVo appUserCardVo = iAppUserCardService.queryByCardNo(cardNo);
        if(appUserCardVo != null){
            return R.fail("当前卡号已存在");
        }
        // 保存储蓄卡信息
        AppUserCardBo appUserCard = new AppUserCardBo();
        appUserCard.setCardNo(cardNo);
        appUserCard.setCardBank(cardBank);
        appUserCard.setPhone(phone);
        appUserCard.setCardType(0L); // 0 储蓄卡  1 信用卡
        appUserCard.setUserId(Convert.toLong(id));
        iAppUserCardService.insertByBo(appUserCard);
        return R.ok();
    }

}
